const express=require('express');
const system=express.Router();
const axios = require('axios')
const qs = require('querystring')
const multipart = require('connect-multiparty');
const FormData = require('form-data')
var multipartMiddleware = multipart()
const fs = require("fs");
const jwt = require("jsonwebtoken")//解析token

// 跳转到广告管理页面
system.get('/advert',(req, res)=>{
    res.render('admin/advert',{
        userInfo: jwt.decode(req.session.token)
    });
});
// 跳转到轮播图管理页面
system.get('/advertCarousel',(req, res)=>{
    res.render('admin/advertCarousel',{
        userInfo: jwt.decode(req.session.token)
    });
});
// 查询所有图片
system.get('/findAllHead', (req, res) => {
    axios({
        url: 'consumer/findAllHead',
        method: 'GET',
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 给轮播图排序
system.post('/rotationSort', (req, res) => {
    let {list} = req.query;
    axios({
        url: 'consumer/rotationSort',
        method: 'PUT',
        params: {
            list:list
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 修改一个轮播图的图片
system.post('/findAllHead',multipartMiddleware,(req,res)=>{
    let file = req.files.image;
    let rotationId = req.query.rotationId;
    console.log("-----");
    console.log(rotationId);
    console.log(file.headers)
    const uri = file.path
    console.log(uri);
    let multipartFile = new FormData();
    multipartFile.append('image', fs.createReadStream(uri));
    axios.post(`consumer/updateAHead?rotationId=${rotationId}`, multipartFile, { headers: multipartFile.getHeaders() }).then((data) => {
        console.log("success============")
        res.send({ data: data.data })
    }).catch((err) => {
        console.log("err");
        res.send(err)
    })
})
// 跳转到品牌管理
system.get('/brandMenu',(req, res)=>{
    res.render('admin/brandMenu',{
        userInfo: jwt.decode(req.session.token)
    });
});



// 跳转到标签管理
system.get('/expressMenu',(req, res)=>{
    res.render('admin/expressMenu',{
        userInfo: jwt.decode(req.session.token)
    });
});
// 查询所有快递品牌
system.get('/queryExpressBrand', (req, res) => {
    axios({
        url: 'ExpressBrand/queryExpressBrand',
        method: 'GET',
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 新增快递品牌
system.post('/addExpressBrand', (req, res) => {
    let {expressBrand} = req.query;
    axios({
        url: 'ExpressBrand/addExpressBrand',
        method: 'POST',
        params: {
            expressBrand:expressBrand,
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 删除快递品牌
system.delete('/deleteExpressBrand', (req, res) => {
    let {expressBrandId} = req.query;
    console.log(req.query);
    axios({
        url: 'ExpressBrand/deleteExpressBrand',
        method: 'DELETE',
        params: {
            expressBrandId:expressBrandId
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 更改快递品牌
system.put('/updateExpressBrand', (req, res) => {
    let {expressBrand,expressBrandId} = req.query;
    console.log(req.query);
    axios({
        url: 'ExpressBrand/updateExpressBrand',
        method: 'PUT',
        params: {
            ExpressBrand:expressBrand,
            expressBrandId:expressBrandId
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 跳转到标签管理
system.get('/labelMenu',(req, res)=>{
    res.render('admin/labelMenu',{
        userInfo: jwt.decode(req.session.token)
    });
});
// 查询所有快递标签
system.get('/queryExpressType', (req, res) => {
    axios({
        url: 'expressType/queryExpressType',
        method: 'GET',
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 新增快递标签
system.post('/addExpressType', (req, res) => {
    let {expressBrand} = req.query;
    axios({
        url: 'expressType/addExpressType',
        method: 'POST',
        params: {
            expressType:expressBrand,
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 删除快递品牌
system.delete('/deleteExpressType', (req, res) => {
    let {expressBrandId} = req.query;
    console.log(req.query);
    axios({
        url: 'expressType/deleteExpressType',
        method: 'DELETE',
        params: {
            expressTypeId:expressBrandId
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})
// 更改快递品牌
system.put('/updateExpressType', (req, res) => {
    let {expressBrand,expressBrandId} = req.query;
    console.log(req.query);
    axios({
        url: 'expressType/updateExpressType',
        method: 'PUT',
        params: {
            expressType:expressBrand,
            expressTypeId:expressBrandId
        }
    })
    .then(data => {
        console.log(data.data);
        res.send(data.data)
    })
    .catch(err => {
        console.log(err.config);
    })
})

module.exports=system;



